const now = new Date() //当前日期
const nowMonth = now.getMonth() //当前月
let nowYear = now.getYear() //当前年
nowYear += (nowYear < 2000) ? 1900 : 0 //
const formatDate = date => {
    let myyear = date.getFullYear()
    let mymonth = date.getMonth() + 1
    let myweekday = date.getDate()

    if (mymonth < 10) {
        mymonth = "0" + mymonth
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday
    }
    return (myyear + "-" + mymonth + "-" + myweekday)
}
const getMonthDays = myMonth => {
    var monthStartDate = new Date(nowYear, myMonth, 1)
    var monthEndDate = new Date(nowYear, myMonth + 1, 1)
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
    return days
}
module.exports = {
    getWeek() {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        return { start: formatDate(start), end: formatDate(end) }
    },
    getNowTime() {
        return `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
    },
    getMonthStartDate: () => { //获得本月的开端日期
        const monthStartDate = new Date(nowYear, nowMonth, 1)
        return formatDate(monthStartDate)
    },
    getMonthEndDate: () => { //获得本月的停止日期
        const monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth))
        return formatDate(monthEndDate)
    },
    getFilterMonthLastDay: (year, month) => {
        let new_year = year //取当前的年份 
        let new_month = month++ //取下一个月的第一天，方便计算（最后一天不固定） 
        if (month > 12) {
            new_month -= 12 //月份减 
            new_year++ //年份增 
        }
        const new_date = new Date(new_year, new_month, 1) //取当年当月中的第一天 
        return (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate() //获取当月最后一天日期 
    },
}




